<%--
  Created by IntelliJ IDEA.
  User: hongfive
  Date: 2019/1/8
  Time: 20:44
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basepath = request.getScheme()
            + "://" + request.getServerName()
            + ":" + request.getServerPort() + "/" + path;
%>
<html>
<head>
    <meta charset="UTF-8">
    <title>油画列表</title>
    <link rel="stylesheet" href="<%=basepath%>/statics/backcss/css/index.css">
    <link rel="stylesheet" href="<%=basepath%>/statics/backcss/css/bootstrap.min.css">
    <script src="<%=basepath%>/statics/backcss/js/jquery.min.js"></script>
</head>
<script>
    // 规范时间格式Date()类型
    function formatDate(now) {
        var now = new Date(now);
        var year = now.getFullYear();
        var month = now.getMonth() + 1;
        var date = now.getDate();
        var hour = now.getHours();
        var minute = now.getMinutes();
        var second = now.getSeconds();
        return year + "年" + fixZero(month, 2) + "月" + fixZero(date, 2)
            + "日" + fixZero(hour, 2) + ":" + fixZero(minute, 2) + ":"
            + fixZero(second, 2);
    }
    //时间如果为单位数补0
    function fixZero(num, length) {
        var str = "" + num;
        var len = str.length;
        var s = "";
        for (var i = length; i-->len;) {
            s += "0";
        }
        return s + str;
    }
    //获取地址栏中的键值对
    function getUrlPara(paraName){
        var sUrl = location.href;
        var sReg = "(?:\\?|&){1}"+paraName+"=([^&]*)"
        var re=new RegExp(sReg,"gi");
        re.exec(sUrl);
        return RegExp.$1;
    }

    function del(cid) {
        var msg="开始";
        $.ajax({
            url:"../canvas/Cdelete",
            dataType:'json',
            data:{
                cid: cid
            },
            async:false,
            type:'post',
            success:function (data) {
                var json=data;
                if (json.code==1){
                    msg="删除成功"
                    alter(msg);
                }
                else {
                    msg=json.message;
                }
            },
            error: function(er){
                alter(er);
            }
        })
    }

    $(function () {
        var re_data = new Object();
        var c_data=new Object();
        $.ajax({
            url:'../category/Cglist',
            dataType: 'json',
            async:false,
            type:"GET",
            contentType:'application/json;charset=utf-8',
            success: function(data){
                var json = data;
                console.log(json);
                if (json.code == 1){
                    console.log(json.data);
                    re_data = json.data;
                    //展示分类名
                    for (var i=0;i<re_data.length;i++){
                        var com=" <input type=\"hidden\" name=\"cgid\" id=\"cgid\" value=\""+re_data[i].cgid+"\">\n" +
                            "<a href=\"../canvas/index?cgid="+re_data[i].cgid+"\" >"+re_data[i].cgName+"</a>";
                        $("#Clist").append(com);
                    }
                }
                else {
                    re_data=json.message;
                }
                console.log(re_data);
            },
            error: function (er) {
                console.log(er);
                alert(er)
            }
        })
        //得到url中的cgid属性值
        var cgid=getUrlPara("cgid");
        $.ajax({
            url:'../canvas/Clist',
            dataType: 'json',
            data:{
                cgid:cgid
            },
            async:false,
            type:"GET",
            success: function(data){
                var json = data;
                console.log(json);
                if (json.code == 1){
                    console.log(json.data);
                    c_data = json.data;
                    //展示油画
                    for (var i=0;i<c_data.length;i++){
                        var dom="<tr>\n" +
                            "                    <td>"+c_data[i].name+"</td>\n" +
                            "                    <td>"+c_data[i].creator+"</td>\n" +
                            "                    <td>￥"+c_data[i].price+"</td>\n" +
                            "                    <td> "+formatDate(c_data[i].createTime-(14*60*60*1000))+"</td>\n" +
                            "                    <td>"+formatDate(c_data[i].updateTime-(14*60*60*1000))+"</td>\n" +
                            "                    <td>"+c_data[i].description+"</td>\n" +
                            "                    <td><a href=\"#\">编辑</a></td>\n" +
                            "                    <td><a href=\"\" onclick='del("+c_data[i].cid+")'>删除</a>\n" +
                            "                        </tr>"
                        $("#canvasL").append(dom);
                    }
                }
                else {
                    c_data=json.message;
                }
                console.log(re_data);
            },
            error: function (er) {
                console.log(er);
                alert(er)
            }
        })
    })
</script>
<body>
<header>
    <div class="container">
        <nav id="Clist">
        </nav>
        <nav>
            <a href="../category/index" >分类</a>
            <a href="../canvas/listdo">主页</a>
        </nav>
        <nav>
            <a href="#">登录</a>
            <a href="#" onclick="alert('功能暂未开放');">注册</a>
        </nav>
    </div>
</header>
<section class="banner">
    <div class="container">
        <div>
            <h1>油画</h1>
            <p>油画列表</p>
        </div>
    </div>
</section>
<section class="main">
    <div class="container">
        <table class="table table-striped">
            <thead>
            <tr>
                <th>名称</th>
                <th>创作者</th>
                <th>价格</th>
                <th>创建时间</th>
                <th>最后修改时间</th>
                <th>描述</th>
                <th>编辑</th>
                <th>删除</th>
            </tr>
            </thead>
            <tbody id="canvasL">
            </tbody>
        </table>
    </div>
</section>
<section class="page">
    <div class="container">
        <div id="fatie">
            <a href="../canvas/add"><button>新建</button></a>
        </div>
    </div>
</section>
<footer>
</footer>
</body>
</html>
